package com.example.cinema.activity;

import android.os.Build;
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.view.Gravity;
import android.view.View;
import android.widget.ImageView;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.databinding.DataBindingUtil;

import com.example.cinema.R;
import com.example.cinema.UserAttribute;
import com.example.cinema.databinding.ActivityPasswordBinding;
import com.example.cinema.request.RequestSend;

import java.util.Timer;
import java.util.TimerTask;

//修改密码
public class ActivityUserPassword extends AppCompatActivity {

    private ActivityPasswordBinding APB;
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        APB = DataBindingUtil.setContentView(this, R.layout.activity_password);
        init();
        if(Build.VERSION.SDK_INT>11 && Build.VERSION.SDK_INT<19)
        {
            View view = getWindow().getDecorView();
            view.setSystemUiVisibility(View.GONE);
        }else if(Build.VERSION.SDK_INT>=19){
            View view = getWindow().getDecorView();
            int UIOP = View.SYSTEM_UI_FLAG_HIDE_NAVIGATION|View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY|View.SYSTEM_UI_FLAG_FULLSCREEN;
            view.setSystemUiVisibility(UIOP);
        }
    }

    private UserAttribute attribute;
    private  RequestSend requestSend;
    private boolean sendstate;
    private void init() {
        requestSend = new RequestSend(getApplicationContext());
        attribute = (UserAttribute) getApplication();
        //返回
        APB.finsh.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        //发送验证
        APB.sendcode.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                if(!sendstate)
                {
                    sendstate = true;
                    if(attribute.getUname()!=null)
                    {
                        String pswd = APB.pswds.getText().toString();
                        if(pswd!=null&&pswd.length()>6)
                        {
                            SendCodes();
                        }else {
                            Toast.makeText(getApplicationContext(),"新密码不能少于6位",Toast.LENGTH_LONG).show();
                            sendstate = false;
                            return;
                        }

                    }
                    //进行发送短信
                    Timers();
                }
            }
        });
        //发送修改
        APB.confirm.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String pswds = APB.pswds.getText().toString();
                String codes = APB.codeeidt.getText().toString();
                if(codes==null||codes.length()<4||pswds.length()<6)
                {
                    Toast.makeText(getApplicationContext(),"密码或验证码输入错误",Toast.LENGTH_LONG).show();
                    APB.confirm.setEnabled(true);
                    return;
                }
                String pswda = APB.pswd.getText().toString();
                if(!pswda.equals(pswds))
                {
                    Toast.makeText(getApplicationContext(),"两次输入密码不一致",Toast.LENGTH_LONG).show();
                    APB.confirm.setEnabled(true);
                    return;
                }
                //发送修改
                SendRequest(pswds,codes);
                APB.confirm.setEnabled(false);
            }
        });
    }

    private Timer timer = new Timer();
    private int times = 60;
    private void Timers()
    {
        TimerTask timerTask = new TimerTask() {
            @Override
            public void run() {
                times--;
                Message message = new Message();
                if(times<=0)
                {
                    sendstate = false;
                    timer.cancel();
                    message.what = 1001;
                    message.obj = "发送验证";
                    handler.sendMessage(message);
                    return;
                }

                message.what = 1001;
                message.obj = String.valueOf(times);
                handler.sendMessage(message);
            }
        };
        timer.schedule(timerTask,1000,1000);
    }


    //发送改密
    private void SendRequest(String pwd,String code)
    {
        new Thread()
        {
            @Override
            public void run() {

                boolean s = requestSend.ReqUserPaswd(pwd,attribute,code);
                Message message = new Message();
                message.what = 1000;
                message.obj = s;
                handler.sendMessage(message);
            }
        }.start();
    }
    //发送验证码
    private void SendCodes()
    {
        new Thread()
        {
            @Override
            public void run() {
                boolean bool = requestSend.SendPhoneCode(attribute.getUname(),attribute);
                Message message = new Message();
                message.what = 1002;
                message.obj = bool;
                handler.sendMessage(message);
            }
        }.start();
    }
    //UI线程
    Handler handler = new Handler(Looper.getMainLooper())
    {
        @Override
        public void handleMessage(@NonNull Message msg) {
            if(msg.what == 1000)
            {
                if((boolean)msg.obj)
                {
                    SuccessToast();
                    APB.confirm.setEnabled(true);
                }else {
                    Toast.makeText(getApplicationContext(),"改密失败,验证码错误！",Toast.LENGTH_LONG).show();
                    APB.confirm.setEnabled(true);
                }
                return;
            }
            if(msg.what == 1001)
            {
                String ss = (String) msg.obj;
                APB.sendcode.setText(ss);
                return;
            }
            if(msg.what== 1002)
            {
                boolean bol = (boolean) msg.obj;
                if(bol)
                {
                    Toast.makeText(getApplicationContext(),"验证码已发送",Toast.LENGTH_LONG).show();
                    return;
                }else {
                    Toast.makeText(getApplicationContext(),"验证码发送失败",Toast.LENGTH_LONG).show();
                }
            }
        }
    };

    private Toast toast;
    private void SuccessToast()
    {
        ImageView imageView = new ImageView(getApplicationContext());
        imageView.setImageResource(R.mipmap.success);
        toast = new Toast(getApplicationContext());
        toast.setView(imageView);
        toast.setGravity(Gravity.CENTER,0,0);
        toast.setDuration(Toast.LENGTH_LONG);
        toast.show();
    }
}
